#pragma once

#include <string>
#include <memory>
#include <nlohmann/json.hpp>
#include "logger/LoggerManager.h"
namespace cpp_backend
{
    namespace config
    {

        /**
         * @brief 服务器配置
         */
        struct ServerConfig
        {
            std::string host = "0.0.0.0";
            int http_port = 8080;
            int websocket_port = 8081;
            int grpc_port = 9090;
            int thread_pool_size = 8;
            int io_thread_count = 4;
        };

        /**
         * @brief MySQL配置
         */
        struct MySQLConfig
        {
            std::string host = "127.0.0.1";
            int port = 3306;
            std::string database = "file_collaboration";
            std::string username = "root";
            std::string password = "";
            int pool_size = 50;
            int connect_timeout = 10; // 秒
        };

        /**
         * @brief Redis配置
         */
        struct RedisConfig
        {
            std::string host = "127.0.0.1";
            int port = 6379;
            std::string password = "";
            int pool_size = 20;
            int connect_timeout = 5; // 秒
            int db_index = 0;
        };

        /**
         * @brief 存储配置
         */
        struct StorageConfig
        {
            std::string upload_path = "/tmp/uploads";
            int64_t max_file_size = 10737418240; // 10GB
            int chunk_size = 5242880;            // 5MB
            bool enable_deduplication = true;    // 启用秒传
        };

        /**
         * @brief JWT配置
         */
        struct JWTConfig
        {
            std::string secret = "default_secret_key";
            int expire_seconds = 7200; // 2小时
        };

        /**
         * @brief 监控配置
         */
        struct MonitorConfig
        {
            bool enable = true;
            int report_interval = 10; // 秒
            bool enable_prometheus = false;
            int prometheus_port = 9090;
        };

        /**
         * @brief 全局配置管理器(单例模式)
         */
        class Config
        {
        public:
            /**
             * @brief 获取单例实例
             */
            static Config &Instance();

            /**
             * @brief 从JSON文件加载配置
             * @param config_file 配置文件路径
             * @return 是否加载成功
             */
            bool LoadFromFile(const std::string &config_file);

            /**
             * @brief 从JSON字符串加载配置
             * @param json_str JSON字符串
             * @return 是否加载成功
             */
            bool LoadFromString(const std::string &json_str);

            /**
             * @brief 保存配置到文件
             * @param config_file 配置文件路径
             * @return 是否保存成功
             */
            bool SaveToFile(const std::string &config_file) const;

            /**
             * @brief 获取配置项
             */
            const ServerConfig &GetServerConfig() const { return server_config_; }
            const MySQLConfig &GetMySQLConfig() const { return mysql_config_; }
            const RedisConfig &GetRedisConfig() const { return redis_config_; }
            const StorageConfig &GetStorageConfig() const { return storage_config_; }
            const JWTConfig &GetJWTConfig() const { return jwt_config_; }
            const MonitorConfig &GetMonitorConfig() const { return monitor_config_; }
            const logger::LogConfig &GetLogConfig() const { return log_config_; }

            /**
             * @brief 设置配置项(用于测试)
             */
            void SetServerConfig(const ServerConfig &config) { server_config_ = config; }
            void SetMySQLConfig(const MySQLConfig &config) { mysql_config_ = config; }
            void SetRedisConfig(const RedisConfig &config) { redis_config_ = config; }
            void SetStorageConfig(const StorageConfig &config) { storage_config_ = config; }
            void SetJWTConfig(const JWTConfig &config) { jwt_config_ = config; }
            void SetMonitorConfig(const MonitorConfig &config) { monitor_config_ = config; }

            /**
             * @brief 打印配置信息(隐藏敏感信息)
             */
            void PrintConfig() const;

        private:
            Config() = default;
            ~Config() = default;
            Config(const Config &) = delete;
            Config &operator=(const Config &) = delete;

            /**
             * @brief 从JSON对象解析配置
             */
            void ParseFromJson(const nlohmann::json &j);

            /**
             * @brief 转换为JSON对象
             */
            nlohmann::json ToJson() const;

            /**
             * @brief 验证配置有效性
             */
            bool ValidateConfig() const;

        private:
            ServerConfig server_config_;
            MySQLConfig mysql_config_;
            RedisConfig redis_config_;
            StorageConfig storage_config_;
            JWTConfig jwt_config_;
            MonitorConfig monitor_config_;
            logger::LogConfig log_config_;
        };

    } // namespace config
} // namespace file_server
